<?php

namespace App\Http\Controllers\Webdisk;

use App\Http\Controllers\Controller;
use App\Webdisk\Model\ContributeInfo;
use Illuminate\Http\Request;

use function App\Webdisk\makeRes;

class ContributeController extends Controller
{
    function upload(Request $req){
        $savePath = env("TEMP_DIR","");
        if($savePath == ''){
            return makeRes(-3,null,"服务器未配置该项");
        }
        $req->validate([
            'path'=>['required'],
            'file_describe'=>['required'],
            'email'=>['required','email'],
            'file'=>['required','file']
        ]);
        $fileName = $_FILES['file']['name'];
        $md5 = md5(time().random_int(0,PHP_INT_MAX));

        $model = new ContributeInfo();
        $model->file = $fileName;
        $model->email = $_POST['email'];
        $model->path = $_POST['path'];
        $model->status = 0;
        $model->md5 = $md5;
        $model->file_describe = $_POST['file_describe'];

        if(!$model->save()){
            return makeRes(-2,"error","数据库错误");
        }else if(move_uploaded_file($_FILES['file']['tmp_name'],"$savePath/$md5")){
            return makeRes();
        }else{
            return makeRes(-2,'error',"文件系统错误");
        }
    }
}
